package com.major.crowd.controller;

import com.major.crowd.api.MysqlRemoteService;
import com.major.crowd.constant.CrowdConstant;
import com.major.crowd.util.ResultEntity;
import com.major.crowd.vo.AddressVO;
import com.major.crowd.vo.MemberLoginVO;
import com.major.crowd.vo.OrderProjectVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private MysqlRemoteService mysqlRemoteService;

    Logger logger = LoggerFactory.getLogger(OrderController.class);

    @RequestMapping("/save/address")
    public String saveAddress(
            AddressVO addressVO,
            HttpSession session
    ){
        // 保存地址信息
        ResultEntity<String> resultEntity = mysqlRemoteService.saveAddressRemote(addressVO);

        logger.info("地址保存处理结果："+resultEntity.getResult());

        // 获取对象
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");

        // 获取returnCount
        Integer returnCount = orderProjectVO.getReturnCount();

        // 重定向，重新进入确认订单页面
        return "redirect:http://localhost/order/confirm/order/" + returnCount;
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String showReturnConfirmInfo(
            @PathVariable("returnCount") Integer returnCount,
            HttpSession session
    ){
        // 把接收到的数据合并到session域
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");

        orderProjectVO.setReturnCount(returnCount);

        session.setAttribute("orderProjectVO",orderProjectVO);

        // 获取用户id
        MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer id = memberLoginVO.getId();

        // 查询收货地址
        ResultEntity<List<AddressVO>> resultEntity = mysqlRemoteService.getAddressVORemote(id);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){
            List<AddressVO> list = resultEntity.getData();
            session.setAttribute("addressVOList",list);
        }

        return "confirm-order";
    }

    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(
            @PathVariable("projectId") Integer projectId,
            @PathVariable("returnId") Integer returnId,
            HttpSession session
    ){
        ResultEntity<OrderProjectVO> resultEntity = mysqlRemoteService.getOrderProjectVORemote(projectId,returnId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){

            OrderProjectVO orderProjectVO = resultEntity.getData();

            session.setAttribute("orderProjectVO",orderProjectVO);
        }
        return "confirm-return";
    }
}
